import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.BoxLayout;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import java.awt.GridBagLayout;
import javax.swing.JTree;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.JButton;
import javax.swing.JSplitPane;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.Component;
import javax.swing.Box;
import java.awt.Insets;

import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JInternalFrame;
import javax.swing.JLayeredPane;
import javax.swing.JList;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;
import javax.swing.SpringLayout;
import javax.swing.JPasswordField;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.AbstractListModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;

import client.Client;

import file.DirectoryReader;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeSelectionEvent;


public class EjsGui{

	private JFrame frame;
	static JTree tree = new JTree();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		try {
			UIManager.setLookAndFeel("com.jtattoo.plaf.texture.TextureLookAndFeel");
		} catch (Exception e) {
		    // If Nimbus is not available, you can set the GUI to another look and feel.
		}
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EjsGui window = new EjsGui();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public EjsGui() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setResizable(false);
		frame.setBounds(100, 100, 700, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frame.getContentPane().add(menuBar, BorderLayout.NORTH);
		
		JMenu mnMenu = new JMenu("Menu");
		menuBar.add(mnMenu);
		
		JMenuItem mntmConnection = new JMenuItem("Connection");
		mntmConnection.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				 
			}
		});
		mnMenu.add(mntmConnection);
		
		JMenuItem mntmDeconnection = new JMenuItem("Deconnection");
		mntmDeconnection.setEnabled(false);
		mnMenu.add(mntmDeconnection);
		
		JMenuItem mntmQuitter = new JMenuItem("Quitter");
		mnMenu.add(mntmQuitter);
		
		final JPanel panel = new JPanel();
		frame.getContentPane().add(panel, BorderLayout.CENTER);
		SpringLayout sl_panel = new SpringLayout();
		panel.setLayout(sl_panel);
		
		final JPanel panel_1 = new JPanel();
		sl_panel.putConstraint(SpringLayout.NORTH, panel_1, 41, SpringLayout.NORTH, panel);
		sl_panel.putConstraint(SpringLayout.WEST, panel_1, 10, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.EAST, panel_1, 274, SpringLayout.WEST, panel);
		panel.add(panel_1);
		
		JPanel panel_2 = new JPanel();
		sl_panel.putConstraint(SpringLayout.NORTH, panel_2, 41, SpringLayout.NORTH, panel);
		sl_panel.putConstraint(SpringLayout.SOUTH, panel_2, -125, SpringLayout.SOUTH, panel);
		sl_panel.putConstraint(SpringLayout.SOUTH, panel_1, 0, SpringLayout.SOUTH, panel_2);
		sl_panel.putConstraint(SpringLayout.WEST, panel_2, 383, SpringLayout.WEST, panel);
		sl_panel.putConstraint(SpringLayout.EAST, panel_2, -10, SpringLayout.EAST, panel);
		panel.add(panel_2);
		
		JButton btnNewButton = new JButton("Download");
		sl_panel.putConstraint(SpringLayout.NORTH, btnNewButton, 168, SpringLayout.NORTH, panel);
		sl_panel.putConstraint(SpringLayout.WEST, btnNewButton, 16, SpringLayout.EAST, panel_1);
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("Rafraichir");
		sl_panel.putConstraint(SpringLayout.SOUTH, btnNewButton_1, -6, SpringLayout.NORTH, panel_2);
		sl_panel.putConstraint(SpringLayout.EAST, btnNewButton_1, 0, SpringLayout.EAST, panel_2);
		panel.add(btnNewButton_1);
		
		JLabel lblLocal = new JLabel("Local");
		sl_panel.putConstraint(SpringLayout.WEST, lblLocal, 0, SpringLayout.WEST, panel_1);
		sl_panel.putConstraint(SpringLayout.SOUTH, lblLocal, -6, SpringLayout.NORTH, panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		final JScrollPane scrollPane = new JScrollPane();
		panel_1.add(scrollPane, BorderLayout.CENTER);

		tree.addMouseListener(new MouseAdapter() {
	      public void mouseClicked(MouseEvent me) {
	    		  ToolsGui.doMouseClicked(me, tree);
          }
		 });
		
		panel.add(lblLocal);
		
		JLabel lblDistant = new JLabel("Distant");
		sl_panel.putConstraint(SpringLayout.NORTH, lblDistant, 0, SpringLayout.NORTH, lblLocal);
		sl_panel.putConstraint(SpringLayout.WEST, lblDistant, 0, SpringLayout.WEST, panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JList list = new JList();
		list.setModel(new AbstractListModel() {
			String[] values = new String[] {};
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		list.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
			}
		});
		panel_2.add(list, BorderLayout.CENTER);
		panel.add(lblDistant);
		
		JLabel label = new JLabel("<-------");
		sl_panel.putConstraint(SpringLayout.NORTH, label, 6, SpringLayout.SOUTH, btnNewButton);
		sl_panel.putConstraint(SpringLayout.WEST, label, 10, SpringLayout.WEST, btnNewButton);
		panel.add(label);
		
		JButton btnNewButton_2 = new JButton("Parcourir");
		btnNewButton_2.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser chooser = new JFileChooser();
		        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		        int returnVal = chooser.showOpenDialog(panel);
		        if(returnVal == JFileChooser.APPROVE_OPTION) {
		           System.out.println("You chose to open this file: " + chooser.getSelectedFile().getAbsolutePath());		           
		           		//String root = chooser.getSelectedFile().getAbsolutePath();
		           		DefaultMutableTreeNode root = new DefaultMutableTreeNode("Root");
		           		ToolsGui.remplirTree(chooser.getSelectedFile(), root, tree);
		        		scrollPane.setViewportView(tree);
		        		panel_1.updateUI();
	

		        }
			}
		});
		sl_panel.putConstraint(SpringLayout.NORTH, btnNewButton_2, 15, SpringLayout.SOUTH, panel_1);
		sl_panel.putConstraint(SpringLayout.WEST, btnNewButton_2, 0, SpringLayout.WEST, panel_1);
		sl_panel.putConstraint(SpringLayout.EAST, btnNewButton_2, 0, SpringLayout.EAST, panel_1);
		panel.add(btnNewButton_2);
	}
}
